package utils

import (
	myjwt "dc-news-admin-api/common/middleware/jwt"
	jwtgo "github.com/dgrijalva/jwt-go"
	"github.com/spf13/cast"
	"time"
)

// GenerateToken 生成token
func GenerateToken(id int, role string) (token string, err error) {
	j := &myjwt.JWT{
		SigningKey: []byte("deepcode"),
	}
	claims := myjwt.CustomClaims{
		Role: role,
		StandardClaims: jwtgo.StandardClaims{
			Id:        cast.ToString(id),
			NotBefore: time.Now().Unix() - 1000,                  // 签名生效时间
			ExpiresAt: time.Now().Add(time.Hour * 24 * 7).Unix(), // 过期时间 7天
			Issuer:    "deepcode",                                //签名的发行者
		},
	}

	token, err = j.CreateToken(claims)
	return
}
